package com.yungu.swift.report.controller;

import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminReportParam;
import com.yungu.swift.report.entity.ReportUserRegisterDay;
import com.yungu.swift.report.service.UserRegisterDayService;
import com.yungu.swift.report.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 用户注册数控制器
 */
@Slf4j
@RequestMapping(value = "/report/userRegisterDay")
@RestController
@Api(value = "【用户注册】", tags = "用户注册")
public class UserRegisterDayCtrl {

    @Autowired
    private UserRegisterDayService registerDayService;

    @PostMapping("/queryIndex")
    @ApiOperation(value = "首页统计 — 昨日 / 每日新注册用户量", httpMethod = "POST")
    public ResponseData<List<ReportUserRegisterDay>> queryIndex(@RequestBody AdminReportParam reportParam) {
        return registerDayService.queryIndex(reportParam);
    }

    @PostMapping("/queryPage")
    @ApiOperation(value = "新用户注册分页查询", httpMethod = "POST")
    public ResponseData<Map<String, Object>> queryPage(@RequestBody AdminReportParam reportParam) {
        return registerDayService.queryPage(reportParam);
    }

    @PostMapping("/export")
    @ApiOperation(value = "新用户注册分页导出", httpMethod = "POST")
    public void export(@RequestBody AdminReportParam reportParam, HttpServletResponse response) throws Exception {
        List<ReportUserRegisterDay> list = this.registerDayService.list(reportParam).getData();
        if (CollectionUtils.isEmpty(list)) {
            log.warn("{} 报表,没有统计数据可导出", "新用户注册");
        }
        String templatePath = "/template/user_register_day_report.xlsx";
        ExcelUtil.download(reportParam, response, list, ReportUserRegisterDay.class, templatePath, "新用户注册");
    }
}
